#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QColorDialog> // 颜色对话框
#include <QFileDialog> // 文件对话框
#include <QInputDialog> // 输入对话框: 文本getText，整数getInt，浮点数getDouble，条目getItem
#include <QDir> //
#include <QStringList>
#include <QMessageBox> // 提示对话框: 提问对话框question，通知对话框information，警告对话框warning，关键提示对话框critical
#include <QProgressDialog> //

#include <QErrorMessage>
#include <QWizard>
#include <QLabel>
#include <QVBoxLayout>
#include <QRadioButton>
#include <QButtonGroup>
#include <QHBoxLayout>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    err_ = new QErrorMessage();
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_color_clicked()
{
//    QColorDialog colorDialog(Qt::blue, this);
//    colorDialog.setOptions(QColorDialog::ShowAlphaChannel);
//    colorDialog.exec();
//    // QColor 用来输出 QColorDialog 选择了什么颜色的： QColor(ARGB 1, 0.313726, 0.34902, 1)
//    QColor color = colorDialog.currentColor();
//    qDebug() << "color is " << color << Qt::endl;

    QColor color = QColorDialog::getColor(Qt::blue, this, tr("选择颜色"), QColorDialog::ShowAlphaChannel);
    qDebug() << "color is " << color << Qt::endl;
}


void MainWindow::on_file_clicked()
{
    QString path = QDir::currentPath();
    QString title = tr("文件对话框");
    QString filter = tr("文本文件(*.txt);;图片文件(*.jpg *.gif *.png);;所有文件(*。*)");
    QString aFileName = QFileDialog::getOpenFileName(this, title, path, filter);

    qDebug() << aFileName << Qt::endl;
}


void MainWindow::on_input_clicked()
{
    bool ok = false;
    auto text = QInputDialog::getText(this, tr("文字输入对话框"), tr("请输入用户姓名"), QLineEdit::Normal, tr("admin"), &ok);
    if(ok == true){
        qDebug() << text << Qt::endl;
    }
}


void MainWindow::on_dir_clicked()
{
    bool ok = false;
    auto intdata = QInputDialog::getInt(this, tr("数字输入对话框"), tr("请输入数字"), 200, 200, 400, 10, &ok);
    if(ok == true){
        qDebug() << intdata << Qt::endl;
    }
}


void MainWindow::on_pushButton_clicked()
{
    bool ok = false;
    auto floatdata = QInputDialog::getDouble(this, tr("浮点数输入对话框"), tr("请输入浮点数"), 0.1, -1, 1, 2, &ok);
    if(ok == true){
        qDebug() << floatdata << Qt::endl;
    }
}


void MainWindow::on_pushButton_2_clicked()
{
    QStringList items;
    items << tr("条目1") << tr("条目2");
    bool ok = false;
    auto itemData = QInputDialog::getItem(this, tr("条目输入对话框"), tr("输入或选择条目"), items, 0, true, &ok);
    if(ok == true){
        qDebug() << "item is " << itemData << Qt::endl;
    }
}


void MainWindow::on_pushButton_3_clicked()
{
    auto ret = QMessageBox::question(this, tr("提问对话框"), tr("你是单身吗"), QMessageBox::Yes, QMessageBox::No);
    if(ret == QMessageBox::Yes || ret == QMessageBox::No){
        qDebug() << "ret is " << ret << Qt::endl;
    }

    auto ret2 = QMessageBox::information(this, tr("通知对话框"), tr("你是单身"), QMessageBox::Yes);
    if(ret2 == QMessageBox::Yes){
        qDebug() << "ret2 is " << ret2 << Qt::endl;
    }

    auto ret3 = QMessageBox::warning(this, tr("警告对话框"), tr("你最好找个地方发泄一下"), QMessageBox::Yes);
    if(ret3 == QMessageBox::Yes){
        qDebug() << "ret3 is " << ret3 << Qt::endl;
    }

    auto ret4 = QMessageBox::critical(this, tr("关键提示对话框"), tr("我梦寐以求是真爱和自由"), QMessageBox::Yes);
    if(ret4 == QMessageBox::Yes){
        qDebug() << "ret4 is " << ret4 << Qt::endl;
    }
}

// 当我们复制或进行文件传输时，会显示一个进度条的对话框，我们可以使用QProcessDialog来操作。
void MainWindow::on_pushButton_4_clicked()
{
    /*
//     进度提示框一闪而逝，不是逻辑问题，是 5000 量级太小了
    QProgressDialog progressDialog(tr("正在复制"), tr("取消复制"), 0, 5000, this);
    progressDialog.setWindowTitle(tr("文件复制进度对话框"));
    progressDialog.setWindowModality(Qt::ApplicationModal);
    progressDialog.show();

    for(int i = 0; i < 5000; i++){
        progressDialog.setValue(i);
        QApplication::processEvents();
        if(progressDialog.wasCanceled() == true){
            break;
        }
    }

    progressDialog.setValue(5000);
   */


    // 因为循环很快完成，所以进度条一闪而逝，那么我们可以通过定时器的方式定时更新进度，这样就可以更清楚的看到进度条对话框更新情况了
    progressDialog_ = new QProgressDialog(tr("正在复制"), tr("取消复制"), 0, 5000, this);
    progressDialog_->setWindowTitle(tr("文件复制进度对话框"));
    progressDialog_->setWindowModality(Qt::ApplicationModal);
    timer_ = new QTimer(this);
    connect(timer_, &QTimer::timeout, this, &MainWindow::on_updateProgressDialog);
    connect(progressDialog_, &QProgressDialog::canceled, this, &MainWindow::on_cancelProgressDialog);
    timer_->start(2);
    qDebug("复制结束");
}
//上面代码连接了定时器的timeout信号和主窗口的on_updateProgressDialog函数, 该函数的主要作用就是定时更新进度条，逻辑如下
void MainWindow::on_updateProgressDialog()
{
    count_++;
    if(count_ > 5000){
        timer_->stop();
        delete  timer_;
        timer_ = nullptr;
        delete progressDialog_;
        progressDialog_ = nullptr;
        count_ = 0;
        return;
    }

    progressDialog_->setValue(count_);
}
//另外我们需要在进度条满了的时候发消息通知主窗口关闭
void MainWindow::on_cancelProgressDialog(){
    timer_->stop();
    delete  timer_;
    timer_ = nullptr;
    delete progressDialog_;
    progressDialog_ = nullptr;
    count_ = 0;
    return;
}
//这样进度条满了之后我们就可以回收定时器资源了。

void MainWindow::on_pushButton_5_clicked()
{
    err_->setWindowTitle(tr("错误提示对话框"));
    err_->showMessage(tr("你犯了个错"));
}


void MainWindow::on_pushButton_6_clicked()
{
    QWizard wizard(this);
    wizard.setWindowTitle(tr("全城热恋"));
    QWizardPage* page1 = new QWizardPage();
    page1->setTitle(tr("婚恋介绍引导程序"));
    auto label1 = new QLabel();
    label1->setText(tr("该程序帮助您找到人生伴侣"));
    QVBoxLayout *layout = new QVBoxLayout();
    layout->addWidget(label1);
    page1->setLayout(layout);
    wizard.addPage(page1);


    QWizardPage* page2 = new QWizardPage();
    page2->setTitle("选择心动类型");
    QButtonGroup *group = new QButtonGroup(page2);
    QRadioButton * btn1 = new QRadioButton();
    btn1->setText("白富美");
    group->addButton(btn1);
    QRadioButton * btn2 = new QRadioButton();
    btn2->setText("萝莉");
    group->addButton(btn2);
    QRadioButton * btn3 = new QRadioButton();
    btn3->setText("御姐");
    group->addButton(btn3);
    QRadioButton * btn4 = new QRadioButton();
    btn4->setText("小家碧玉");
    group->addButton(btn4);
    QRadioButton * btn5 = new QRadioButton();
    btn5->setText("女汉子");
    group->addButton(btn5);
    QRadioButton * btn6 = new QRadioButton();
    btn6->setText("成年人不做选择，全选!");
    group->addButton(btn6);
    QVBoxLayout *vboxLayout2 = new QVBoxLayout();
    for(int i = 0; i < group->buttons().size(); i++){
        vboxLayout2->addWidget(group->buttons()[i]);
    }
    page2->setLayout(vboxLayout2);
    wizard.addPage(page2);


    QWizardPage* page3 = new QWizardPage();
    page3->setTitle(tr("你的缘分即将到来"));
    auto label3 = new QLabel();
    label3->setText(tr("感谢您的参与，接下来的一个月会遇到对的人"));
    QVBoxLayout *layout3 = new QVBoxLayout();
    layout3->addWidget(label3);
    page3->setLayout(layout3);
    wizard.addPage(page3);


    wizard.show();
    wizard.exec();
}

